package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.li.work.mapper.SysUserMapper;
import com.ruoyi.teaching.domain.PlatformDDraw;
import com.ruoyi.teaching.domain.UniversalCreateUpdate;
import com.ruoyi.teaching.mapper.PlatformDDrawMapper;
import com.ruoyi.teaching.service.PlatformDDrawService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.teaching.utils.UniversalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2025-01-10
 */
@Service
public class PlatformDDrawServiceImpl extends ServiceImpl<PlatformDDrawMapper, PlatformDDraw> implements PlatformDDrawService {
    @Autowired
    private PlatformDDrawMapper platformDDrawMapper;
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public AjaxResult drawList(Integer pageNum, Integer pageSize) {
        Long userId = null;
        try {
            userId = SecurityUtils.getUserId();
        } catch (ServiceException e) {
            return new AjaxResult(200, "请先登录");
        }
        Page<PlatformDDraw> platformDDrawPage = new Page<>(pageNum, pageSize);
        QueryWrapper<PlatformDDraw> platformDDrawQueryWrapper = new QueryWrapper<>();
        platformDDrawQueryWrapper.eq("create_id", userId);
        platformDDrawQueryWrapper.orderByDesc("create_time");
        Page<PlatformDDraw> platformDDrawPage1 = platformDDrawMapper.selectPage(platformDDrawPage, platformDDrawQueryWrapper);
        return new AjaxResult(200, "查询成功", platformDDrawPage1);
    }

    @Override
    public AjaxResult detailDraw(Long id) {
        PlatformDDraw platformDDraw = platformDDrawMapper.selectById(id);
        if (platformDDraw == null) {
            throw new RuntimeException("该作品不存在");
        }
        return new AjaxResult(200, "查询成功", platformDDraw);
    }

    @Override
    public AjaxResult addOrUpdateDraw(PlatformDDraw platformDDraw) {

       if(platformDDraw.getId() == null){
           UniversalCreateUpdate createUpdate = UniversalUtil.getCreateUpdate(sysUserMapper);
           platformDDraw.setCreateId(createUpdate.getId());
           platformDDraw.setCreateBy(createUpdate.getName());
           platformDDraw.setCreateTime(DateUtils.getNowDate());
           int insert = platformDDrawMapper.insert(platformDDraw);
           if (insert == 0) {
               throw new RuntimeException("新增失败");
           }
           return new AjaxResult(HttpStatus.SUCCESS, "添加成功",platformDDraw.getId());
       }else {
           UniversalCreateUpdate createUpdate = UniversalUtil.getCreateUpdate(sysUserMapper);
           platformDDraw.setUpdateBy(createUpdate.getName());
           platformDDraw.setUpdateId(createUpdate.getId());
           platformDDraw.setUpdateTime(DateUtils.getNowDate());
           int update = platformDDrawMapper.updateById(platformDDraw);
           if (update == 0) {
               throw new RuntimeException("修改失败");
           }
           return new AjaxResult(HttpStatus.SUCCESS, "修改成功");
       }
    }

    @Override
    public AjaxResult deleteDraw(Long id) {
        int i1 = platformDDrawMapper.deleteById(id);
        if(i1 == 0){
            throw new RuntimeException("删除失败");
        }
        return new AjaxResult(HttpStatus.SUCCESS, "删除成功");
    }
}
